package iterator;

import java.util.*;

/**
 * @description: ToDo
 * @author: marin
 * @time: 2020/7/3 16:19
 */
public class SetIterator {
    public static void main(String[] args) {
        Set<String> sets = new HashSet<String>();
        sets.add("jack");
        sets.add("tony");
        sets.add("bad");

        //1.增强for循环遍历
        for (String s:sets){
            System.out.println(s);
        }

        //2.迭代器遍历
        Iterator<String> iterators = sets.iterator();
        while (iterators.hasNext()) {
            String value= iterators.next();
            System.out.println(value);
        }

        //3.集合类通用的遍历方式(早期就存在)
        for (Iterator it2 = sets.iterator(); it2.hasNext(); ) {
            System.out.println(it2.next());
        }

        //4.jdk1.8之后
        sets.forEach(System.out::println);


        Set<Person> personSet = new HashSet<Person>();
        Person person1 = new Person("tongy",19);
        Person person2 = new Person("jack",16);
        Person person3 = new Person("rich",15);
        personSet.add(person1);
        personSet.add(person2);
        personSet.add(person3);

        Iterator<Person> iterator = personSet.iterator();
        Person person=null;
        while (iterator.hasNext()){
            person = iterator.next();
            System.out.println(person.name+":"+person.age);
        }
    }

    static class Person {
        String name;
        int age;
        public Person(){}
        public Person(String name,int age){
            this.name = name;
            this.age = age;
        }
        public void sayName(){
            System.out.println(name);
        }
        public void sayAge(){
            System.out.println(age);
        }
    }
}
